<template>
  <div class="login-account">
    <el-form
      label-width="65px"
      :rules="rules"
      :model="account"
      ref="accountref"
    >
      <el-form-item label="账号" prop="name">
        <el-input v-model="account.name" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="account.password" type="password" show-password />
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive, ref } from 'vue'
import { rules } from '../config/account-config'
import { ElForm } from 'element-plus'
import { useStore } from 'vuex'

import LocalCache from '@/utils/cache'

export default defineComponent({
  setup() {
    //保存store
    const store = useStore()

    const account = reactive({
      name: LocalCache.getCache('name') ?? '',
      password: LocalCache.getCache('password') ?? ''
    })
    //保存element的组件实例对象类型是ElForm
    const accountref = ref<InstanceType<typeof ElForm>>()

    const loginactive = (isKeeoPasswoedL: boolean) => {
      accountref.value?.validate((valid) => {
        //判断是否登录
        if (valid) {
          //判断是否记住密码
          if (isKeeoPasswoedL) {
            LocalCache.setCache('name', account.name)
            LocalCache.setCache('password', account.password)
          } else {
            LocalCache.removeCache('name')
            LocalCache.removeCache('password')
          }
          //开始登录验证
          store.dispatch('loginModule/accountLoginAction', { ...account })
        }
      })
    }

    return {
      account,
      rules,
      accountref,
      loginactive
    }
  }
})
</script>

<style scoped></style>
